
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/physics/mechanics/lagrange.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:36 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Lagrange’s Method in Physics/Mechanics &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../../_static/sympy-notailtext-favicon.ico"/>
    <link href="lagrange.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="Symbolic Systems in Physics/Mechanics" href="symsystem.html" />
    <link rel="prev" title="Kane’s Method in Physics/Mechanics" href="kane.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="symsystem.html" title="Symbolic Systems in Physics/Mechanics"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="kane.html" title="Kane’s Method in Physics/Mechanics"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" accesskey="U">Classical Mechanics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Lagrange’s Method in Physics/Mechanics</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="lagrange-s-method-in-physics-mechanics">
<h1>Lagrange’s Method in Physics/Mechanics<a class="headerlink" href="#lagrange-s-method-in-physics-mechanics" title="Permalink to this headline">¶</a></h1>
<p><a class="reference internal" href="index.html#module-sympy.physics.mechanics" title="sympy.physics.mechanics"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.physics.mechanics</span></code></a> provides functionality for deriving equations of motion
using <a class="reference external" href="https://en.wikipedia.org/wiki/Lagrangian_mechanics">Lagrange’s method</a>.
This document will describe Lagrange’s method as used in this module, but not
how the equations are actually derived.</p>
<section id="structure-of-equations">
<h2>Structure of Equations<a class="headerlink" href="#structure-of-equations" title="Permalink to this headline">¶</a></h2>
<p>In <a class="reference internal" href="index.html#module-sympy.physics.mechanics" title="sympy.physics.mechanics"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.physics.mechanics</span></code></a> we are assuming there are 3 basic sets of equations needed
to describe a system; the constraint equations, the time differentiated
constraint equations and the dynamic equations.</p>
<div class="math notranslate nohighlight">
\[\begin{split}\mathbf{m_{c}}(q, t) \dot{q} + \mathbf{f_{c}}(q, t) &amp;= 0\\
\mathbf{m_{dc}}(\dot{q}, q, t) \ddot{q} + \mathbf{f_{dc}}(\dot{q}, q, t) &amp;= 0\\
\mathbf{m_d}(\dot{q}, q, t) \ddot{q} + \mathbf{\Lambda_c}(q, t)
\lambda + \mathbf{f_d}(\dot{q}, q, t) &amp;= 0\\\end{split}\]</div>
<p>In this module, the expressions formed by using Lagrange’s equations of the
second kind are rearranged into the following form:</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\mathbf{M}(q, t) x = \mathbf{f}(q, \dot{q}, t)\)</span></p>
</div></blockquote>
<p>where in the case of a system without constraints:</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(x = \ddot{q}\)</span></p>
</div></blockquote>
<p>For a constrained system with <span class="math notranslate nohighlight">\(n\)</span> generalized speeds and <span class="math notranslate nohighlight">\(m\)</span> constraints, we
will get n - m equations. The mass-matrix/forcing equations are then augmented
in the following fashion:</p>
<div class="math notranslate nohighlight">
\[\begin{split}x = \begin{bmatrix} \ddot{q} \\ \lambda \end{bmatrix} \\
\mathbf{M}(q, t) &amp;= \begin{bmatrix} \mathbf{m_d}(q, t) &amp;
\mathbf{\Lambda_c}(q, t) \end{bmatrix}\\
\mathbf{F}(\dot{q}, q, t) &amp;= \begin{bmatrix} \mathbf{f_d}(q, \dot{q}, t)
\end{bmatrix}\\\end{split}\]</div>
</section>
<section id="id1">
<h2>Lagrange’s Method in Physics/Mechanics<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>The formulation of the equations of motion in <a class="reference internal" href="index.html#module-sympy.physics.mechanics" title="sympy.physics.mechanics"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.physics.mechanics</span></code></a> using
Lagrange’s Method starts with the creation of generalized coordinates and a
Lagrangian. The Lagrangian can either be created with the <code class="docutils literal notranslate"><span class="pre">Lagrangian</span></code>
function or can be a user supplied function. In this case we will supply the
Lagrangian.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.mechanics</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q1</span><span class="p">,</span> <span class="n">q2</span> <span class="o">=</span> <span class="n">dynamicsymbols</span><span class="p">(</span><span class="s1">&#39;q1 q2&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q1d</span><span class="p">,</span> <span class="n">q2d</span> <span class="o">=</span> <span class="n">dynamicsymbols</span><span class="p">(</span><span class="s1">&#39;q1 q2&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="n">q1d</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">q2d</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<p>To formulate the equations of motion we create a <code class="docutils literal notranslate"><span class="pre">LagrangesMethod</span></code>
object. The Lagrangian and generalized coordinates need to be supplied upon
initialization.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span> <span class="o">=</span> <span class="n">LagrangesMethod</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="p">[</span><span class="n">q1</span><span class="p">,</span> <span class="n">q2</span><span class="p">])</span>
</pre></div>
</div>
<p>With that the equations of motion can be formed.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mechanics_printing</span><span class="p">(</span><span class="n">pretty_print</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">form_lagranges_equations</span><span class="p">()</span>
<span class="go">Matrix([</span>
<span class="go">[2*q1&#39;&#39;],</span>
<span class="go">[2*q2&#39;&#39;]])</span>
</pre></div>
</div>
<p>It is possible to obtain the mass matrix and the forcing vector.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">mass_matrix</span>
<span class="go">Matrix([</span>
<span class="go">[2, 0],</span>
<span class="go">[0, 2]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">forcing</span>
<span class="go">Matrix([</span>
<span class="go">[0],</span>
<span class="go">[0]])</span>
</pre></div>
</div>
<p>If there are any holonomic or non-holonomic constraints, they must be supplied
as keyword arguments (<code class="docutils literal notranslate"><span class="pre">hol_coneqs</span></code> and <code class="docutils literal notranslate"><span class="pre">nonhol_coneqs</span></code> respectively) in a
list of expressions which are equal to zero. Modifying the example above, the
equations of motion can then be generated:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span> <span class="o">=</span> <span class="n">LagrangesMethod</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="p">[</span><span class="n">q1</span><span class="p">,</span> <span class="n">q2</span><span class="p">],</span> <span class="n">hol_coneqs</span><span class="o">=</span><span class="p">[</span><span class="n">q1</span> <span class="o">-</span> <span class="n">q2</span><span class="p">])</span>
</pre></div>
</div>
<p>When the equations of motion are generated in this case, the Lagrange
multipliers are introduced; they are represented by <code class="docutils literal notranslate"><span class="pre">lam1</span></code> in this case. In
general, there will be as many multipliers as there are constraint equations.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">form_lagranges_equations</span><span class="p">()</span>
<span class="go">Matrix([</span>
<span class="go">[ lam1 + 2*q1&#39;&#39;],</span>
<span class="go">[-lam1 + 2*q2&#39;&#39;]])</span>
</pre></div>
</div>
<p>Also in the case of systems with constraints, the ‘full’ mass matrix is
augmented by the <span class="math notranslate nohighlight">\(k_{dc}(q, t)\)</span> matrix, and the forcing vector by the
<span class="math notranslate nohighlight">\(f_{dc}(q, \dot{q}, t)\)</span> vector. The ‘full’ mass matrix is of size
(2n + o) x (2n + o), i.e. it’s a square matrix.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">mass_matrix_full</span>
<span class="go">Matrix([</span>
<span class="go">[1, 0, 0,  0,  0],</span>
<span class="go">[0, 1, 0,  0,  0],</span>
<span class="go">[0, 0, 2,  0, -1],</span>
<span class="go">[0, 0, 0,  2,  1],</span>
<span class="go">[0, 0, 1, -1,  0]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">forcing_full</span>
<span class="go">Matrix([</span>
<span class="go">[q1&#39;],</span>
<span class="go">[q2&#39;],</span>
<span class="go">[  0],</span>
<span class="go">[  0],</span>
<span class="go">[  0]])</span>
</pre></div>
</div>
<p>If there are any non-conservative forces or moments acting on the system,
they must also be supplied as keyword arguments in a list of 2-tuples of the
form <code class="docutils literal notranslate"><span class="pre">(Point,</span> <span class="pre">Vector)</span></code> or <code class="docutils literal notranslate"><span class="pre">(ReferenceFrame,</span> <span class="pre">Vector)</span></code> where the <code class="docutils literal notranslate"><span class="pre">Vector</span></code>
represents the non-conservative forces and torques. Along with this 2-tuple,
the inertial frame must also be specified as a keyword argument. This is shown
below by modifying the example above:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span> <span class="o">=</span> <span class="n">ReferenceFrame</span><span class="p">(</span><span class="s1">&#39;N&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">P</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="s1">&#39;P&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">P</span><span class="o">.</span><span class="n">set_vel</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">q1d</span> <span class="o">*</span> <span class="n">N</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FL</span> <span class="o">=</span> <span class="p">[(</span><span class="n">P</span><span class="p">,</span> <span class="mi">7</span> <span class="o">*</span> <span class="n">N</span><span class="o">.</span><span class="n">x</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span> <span class="o">=</span> <span class="n">LagrangesMethod</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="p">[</span><span class="n">q1</span><span class="p">,</span> <span class="n">q2</span><span class="p">],</span> <span class="n">forcelist</span><span class="o">=</span><span class="n">FL</span><span class="p">,</span> <span class="n">frame</span><span class="o">=</span><span class="n">N</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">LM</span><span class="o">.</span><span class="n">form_lagranges_equations</span><span class="p">()</span>
<span class="go">Matrix([</span>
<span class="go">[2*q1&#39;&#39; - 7],</span>
<span class="go">[    2*q2&#39;&#39;]])</span>
</pre></div>
</div>
<p>Exploration of the provided examples is encouraged in order to gain more
understanding of the <code class="docutils literal notranslate"><span class="pre">LagrangesMethod</span></code> object.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Lagrange’s Method in Physics/Mechanics</a><ul>
<li><a class="reference internal" href="#structure-of-equations">Structure of Equations</a></li>
<li><a class="reference internal" href="#id1">Lagrange’s Method in Physics/Mechanics</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="kane.html"
                        title="previous chapter">Kane’s Method in Physics/Mechanics</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="symsystem.html"
                        title="next chapter">Symbolic Systems in Physics/Mechanics</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/modules/physics/mechanics/lagrange.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="symsystem.html" title="Symbolic Systems in Physics/Mechanics"
             >next</a> |</li>
        <li class="right" >
          <a href="kane.html" title="Kane’s Method in Physics/Mechanics"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" >Classical Mechanics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Lagrange’s Method in Physics/Mechanics</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/physics/mechanics/lagrange.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:36 GMT -->
</html>